package com.dr.service.impl;

import com.dr.Enum.ExcepEnum;
import com.dr.common.ution.DateUtil;
import com.dr.common.ution.StringUtil;
import com.dr.db.SysUserMapper;
import com.dr.exception.ServiceExcep;
import com.dr.pojo.SysUser;
import com.dr.service.SysLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by tugou on 2017/4/23.
 */
@Service
public class SysLoginServiceImpl implements SysLoginService {

    @Autowired
    SysUserMapper SysUserMapper;

    @Override
    public SysUser getSysUserByLoginInfo(SysUser sysUser) throws ServiceExcep{
        List<SysUser> list = SysUserMapper.getSysUser(sysUser);
        if(list == null || list.size()==0){
            throw new ServiceExcep(ExcepEnum.USERERROR.getName(),ExcepEnum.USERERROR.getIndex());
        }
        if (!(list.get(0).getUserPwd().equals(sysUser.getUserPwd()))){
            throw new ServiceExcep(ExcepEnum.PWDERROR.getName(),ExcepEnum.PWDERROR.getIndex());
        }
        return list.get(0);
    }

    @Override
    public void saveSysUser(SysUser user)throws ServiceExcep{
        user.setCreateDate(DateUtil.now());
        user.setState(1);
        SysUser check = new SysUser();
        check.setLoginName(user.getLoginName());
        List<SysUser> list = SysUserMapper.getSysUser(check);
        if(list != null && list.size()>0)
            throw new ServiceExcep(ExcepEnum.LOGINISEXIST.getName(),ExcepEnum.LOGINISEXIST.getIndex());
        int result = SysUserMapper.insertSysUser(user);
        if(result == 0){
            throw new ServiceExcep(ExcepEnum.INSERTDATAEMPTY.getName(),ExcepEnum.INSERTDATAEMPTY.getIndex());
        }
    }

    @Override
    public List<SysUser> getUserList() {
        List<SysUser> list = SysUserMapper.getSysUser(null);
        return list;
    }

    @Override
    public int updateSysUser(SysUser sysUser) throws ServiceExcep {
        sysUser.setCreateDate(DateUtil.now());
        int resultCount=SysUserMapper.updateSysUser(sysUser);
        if (resultCount==0){
            throw new ServiceExcep(ExcepEnum.INSERTDATAEMPTY.getName(),ExcepEnum.INSERTDATAEMPTY.getIndex());
        }
        return resultCount;
    }
}
